#!/usr/bin/perl

# Read user inputted restriction sites
############################################################################

open(RESTRICT, $ARGV[0]) ;  # 
$prot_seq=(); # initialize sequence holder
$seq_count=(); # initialize seq counter -- for hash keys
$rest_count=0;
REST: while (my $line = <RESTRICT>){ # read each line of protein sequence file
chomp $line;
	if ($line =~ /^#/){  # Skip '#' user comments lines
	    next REST;
	 };
	if ($line =~ m/^\s*$/gi){ #if blank line, next read line
	next REST;
	};
	$bad++ while $seq_seq =~ m/[^AGTCU.-]/gi; #check for prohibited characters
	       
	       if ($bad > 0){ 
		print " Non-nucleic acid (AGTUC) and/or space (. - ) character(s) detected in sequence. Program ending.\n\n";
		die;
		};
	$line =~ s/ //gi; # remove spaces
	
@rest_sites_fwd[$rest_count] = $line; # capture restriction sites in array
	$line =~ tr/AGCT/TCGA/;
print "\nrest_line: '$line'\n\n";

	@rest_sites_rev[$rest_count] = $line; # capture restriction site complements in array
$rest_count++; # restriction site counter	
}; # end read user restriction site file loop
our @rest_sites = (@rest_sites_fwd,@rest_sites_rev);
print "\n\n @rest_sites\n\n";

# Riboswitch and Shine-Dalgarno/RBS binding site
##########################################################################
# The effector sequence is a user inputted DNA string that will bind the riboswitch and release the Shine-Dalgarno ribosome binding site from an inhibitory hairpin. The effector sequence must be >= 15 nt. This sequence will be referenced against the user inputted restriction sites to check for conflicts. If a user inputted restriction site is discovered within the user inputted effector sequence, a warning will be printed to terminal.


##########################################################################

open(EFFECTOR, $ARGV[1]) ;  # Riboswitch/OBS effector sequences - user input

OBS: while (my $line = <EFFECTOR>){ # read each line of effector sequence file

      chomp $line;
	if ($line =~ /^#/){  # Skip '#' optional user comment lines
	    next OBS;
	 };

	if ($line =~ m/^\s*$/gi){ #if blank line, next read line
	next OBS;
	};

	$bad++ while $seq_seq =~ m/[^AGTCU]/gi; #check for prohibited characters
	       
	       if ($bad > 0){ 
		print " Non-nucleic acid (AGTUC) detected in effector sequence. Program ending.\n\n";
		die;
		};

	$line =~ s/ //gi; # remove spaces
	$line =~ tr/AGCT/agct/; #make lowercase
	$line =~ tr/Uu/tt/; # if user input is in RNA, change to DNA
	
    $effector .= $line; # capture effector sequence into string

	$line =~ tr/agct/tcga/; # effector complement


     $obs .= $line; # effector binding site in sequence
		
}; # end effector sequence read


#########################################################

#########################################################

@nuc_array = qw(a t g c); #nucleic acid character array

$rbs = aggaggt; # e.coli Shine Dalgarno ribosome binding site - invariable;
$rbs_len = length $rbs; # length of ribosome binding site
$obs_len = length $obs; # effector binding site length

$obs_rev = reverse $effector; # 3'->5' version of effector / complementary effector binding site (obs)
$post_rbs = substr($obs_rev,-8,8); # complementary effector binding site residue downstream of ribosome binding site
$post_len = (length $post_rbs) ; # length of above


$pre_len = 3; # number of complementary effector binding residues upstream of ribosome binding site
$rbs_obs_len = $obs_len - $rbs_len - $post_len-$pre_len; # computed spacer on effector binding to skip ribosome binding site (so complementary bases match up even with the rbs space)
$pre_rbs = substr( $obs_rev , $rbs_obs_len , 3); # effector binding residue upstream of ribosome binding site


#@pre = split "", $pre_rbs;
#@post = split "", $post_rbs;

#my $rand_nuc_1= join '', map $nuc_array[rand @nuc_array], 1;
#my $rand_i = int(rand($pre_len));
#$pre[$rand_i] = $rand_nuc_1;

#my $rand_nuc_2 = join '', map $nuc_array[rand @nuc_array], 1;
#my $rand_j = int(rand($post_len));
#$post[$rand_j] = $rand_nuc_2;


#$post_rbs = join($_,@post); # transform generated DNA sequence to string
#$pre_rbs = join($_,@pre); # transform generated DNA sequence to string




#print "obs: $obs, rev: $effector, temp: $obs_temp, rand: $obs_comp, post: $post_rbs\n\n";

print "obs: $obs, rev: $effector, pre: $pre_rbs, post: $post_rbs\n\n";




$stem = $obs . $pre_rbs . $rbs . $post_rbs;
print "before function rest sites: @rest_sites\n";
check_restrict_sites($stem);

$stem_ref = $stem;
#$stem_ref = s/[^$effector]/\./gi;

$stem_ref =~ s/$obs/"x" x $obs_len /e;
$stem_ref =~ tr/atgc/./;
print "\nstem: $stem\ncon:  $stem_ref\n\n";



### Temporary Function -- Looks for restriction sites. If found returns a 0 otherwise if not found
### Returns a 1.
sub check_restrict_sites {

my $input_string = $_[0];

print "in loop '@rest_sites'\n";
print "\n\ninput: $input_string\n\n";
foreach $element(@rest_sites) {
print "\n\nelement: '$element'\n\n";
if ($input_string =~ m/$element/gi){
print ">>>>>>>>>> a restriction site has been found:$element.\n";
return ;
}
}
return 1;
}




